<?php

namespace app\hannel\command;

use qcloudsms\SmsMultiSender;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Rongcui extends Command
{
    protected function configure()
    {
        $this->setName('rongcui')->setDescription('Command Test');
    }

    protected function execute(Input $input, Output $output)
    {
        $buyer = db('buyer')
            ->alias('buyer')
            ->join('__USER__ user','buyer.lock_uid = user.id')
            ->where([
                'lock_uid' => ['neq',0],
                'unlock' => 0,
                'newtime' => ['egt',strtotime('-3 day',strtotime(date('Y-m-d')))]
            ])
            ->field('lock_uid,count(lock_uid) as num,user.mobile')
            ->group('lock_uid')
            ->select()->toArray();
        $zybbk = db('PotentialCustomer')
            ->alias('PotentialCustomer')
            ->join('__USER__ user','PotentialCustomer.lock_uid = user.id')
            ->where([
                'lock_uid' => ['neq',0],
                'lock_status' => 2,
                'type' => 0,
                'newtime' => ['egt',strtotime('-14 day',strtotime(date('Y-m-d')))]
            ])
            ->field('lock_uid,count(lock_uid) as num,user.mobile')
            ->group('lock_uid')
            ->select()->toArray();
        $product = db('PotentialCustomer')
            ->alias('PotentialCustomer')
            ->join('__USER__ user','PotentialCustomer.lock_uid = user.id')
            ->where([
                'lock_uid' => ['neq',0],
                'type' => ['neq', 0],
                'done' => 0,
                'product_sh' => 2,
                'newtime' => ['egt',strtotime('-7 day',strtotime(date('Y-m-d')))]
            ])
            ->field('lock_uid,count(lock_uid) as num,user.mobile')
            ->group('lock_uid')
            ->select()->toArray();
        $buyer = $this->mage_arr($buyer,$zybbk);
        $buyer = $this->mage_arr($buyer,$product);

//        $phone_list = [];
//        $params_list = [];
        foreach ($buyer as $phone) {
            $list['phone'] = trim($phone['mobile']);
            if (preg_match('/^1\d{10}$/', $list['phone'])) {
//                $phone_list[] = $list['phone'];
//                $params_list[] = $phone['num'];
//                if (count($phone_list) == 20) {
//                    $this->sendsms($phone_list, 96831,$params_list);
//                    $phone_list = [];
//                    $params_list = [];
//                }
                $this->sendsms($list['phone'], 96831,[$phone['num']]);
            }
        }
    }

    private function mage_arr($arr1,$arr2){
        foreach ($arr1 as $key => $value) {
            foreach ($arr2 as $val) {
                if($value['lock_uid'] == $val['lock_uid']) {
                    $arr1[$key]['num'] += $val['num'];
                    break;
                }
            }
        }
        return $arr1;
    }

    /**
     * 发送短信
     * @param $phone_list
     * @param $templid
     */
    private function sendsms($phone_list, $templid,$params_list)
    {
//        $multiSender = new SmsMultiSender('1400047725', 'd9c93046f4f45a7f591914ca5b506923');
//        $result = $multiSender->sendWithParam("86", $phone_list, $templid, $params_list, "", "", "");
//        print_r($result);
    }
}